/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.quantum.jbencoder.types;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;

/**
 *
 * @author g073025
 */
public abstract class BencodedPrimitiveType implements BencodedType {
    /***
     * according with bencoded type specification V1.0
     * all types (except Bencoded String) have a suffix 'e'
     * so, we factorize that suffix for all types
     */
    protected final byte suffix = 'e';
    /***
     * according with Bencoded type specification V1.0
     * data are encoded as UTF-8 charset
     */
    protected final Charset charset = Charset.forName("UTF-8");

    /**
     * serialize BencodedType according with Bencoded Type Specification
     * @param os
     * @throws IOException 
     */
    public void serialize(OutputStream os) throws IOException {
        writePrefix(os);
        writeValue(os);
        writeSuffix(os);
    }

    public abstract void deserialize(InputStream is)  throws IOException;

    /**
     * write prefix of type according with Bencoded Type Specification
     *
     * @param os
     */
    protected abstract void writePrefix(OutputStream os) throws IOException;

    /**
     * write value of type according with Bencoded Type Specification
     *
     * @param os
     */
    protected abstract void writeValue(OutputStream os) throws IOException;

    /**
     * write suffix of type according with Bencoded Type Specification
     *
     * @param os
     */
    protected abstract void writeSuffix(OutputStream os) throws IOException;
}
